﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;

namespace NPCScriptEditor.Classes
            
{
    public class SaveLoad
    {
       public static void Save(List<string> codes,string path)
        {

            if (codes.Count==0)
            {
                MessageBox.Show("啥都没有你保存啥");
            }
            else
            {
string a="";
            var _extension = Path.GetExtension(path);
            switch (_extension)
            {
                case "ns":
                    for (int i = 0; i < codes.Count; i++)
                    {
                        if (i == codes.Count - 1)
                        {
                          a += codes[i];
                        }
                        else { a += codes[i] + "[tab]"; }
                        }
                    break;
                default:
                    if (codes.Count>1)
                    {
                        MessageBox.Show("JS/TXT文件请勿创建多个页");

                    }
                    else
                    {
                        a = codes[0];
                    }
                    break;
            }
           
           
            try {
            File.WriteAllText(path,a);
            }catch(Exception error)
            {
                MessageBox.Show( error.Message);
            }
            }
        }
        public static string[] load(string path) {
            string a=File.ReadAllText(path);
           // Debug.Write(a);
            string[] b= Regex.Split(a, "\\[tab\\]", RegexOptions.IgnoreCase);
            //Debug.Write(b);
            return b;
        }
    }
}
